package com.ws.mbp.springsecuritydemo.service;
import com.ws.mbp.springsecuritydemo.mapper.UsersMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
// 授权决策
@Service
@RequiredArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    UsersMapper usersMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //Spring Security 必须通过 UserDetailsService 获取用户信息，这是框架的强制要求。
        System.out.println("UserDetailsServiceImpl  loadUserByUsername执行了验证 Token 的有效性 + 加载用户权限。。 Spring Security 框架内部自动调用用于授权决策。将用户信息存入 SecurityContextHolder"+username);
        return usersMapper.findByUsername(username)
                .orElseThrow(() -> new UsernameNotFoundException("User not found"));
    }
}